package com.tx.activemq._01_c_p._04_jms_dependence._041_persistent.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 队列 持久化
 */
public class Producer_JMS {

    private static final String DEFAULT_URL = "tcp://192.168.254.30:61616";
    private static final String QUEUE_NAME = "queue_01";

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);
        Connection connection = factory.createConnection();
        //activemq连接默认是关闭的，需要手动开启
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageProducer producer = session.createProducer(queue);
        // 1. 非持久化
//        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        //2. 持久化
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        for (int i = 0; i < 3; i++) {
            TextMessage textMessage = session.createTextMessage("msg" + i);
            producer.send(textMessage);
        }

        producer.close();
        session.close();
        connection.close();
        System.out.println("****推送数据到mq完毕****");
    }
}